<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=9" />
		<meta http-equiv="pragma" content="no-cache" />
		<title>角色管理</title>
		<%@ include file="../../common/Admin-list.jsp"%>  
		<script type="text/javascript" src="<%=bpath %>Scripts/easyui/locale/easyui-treegrid-cascadeCheck.js"></script> 		 
	
		<script type="text/javascript">
		$(function(){			
			//角色
			$('#dataGridSys').datagrid({		
				height:'480',
				width:'480',
				rownumbers:true,//设置为true将显示行数					
				striped:true, //设置为true将交替显示行背景
				fitColumns:true,//设置为true将自动使列适应表格宽度以防止出现水平滚动
				animate:true,
				collapsible:true,
				idField:'id',//id字段
				pagination : true,//分页控件
				pageSize : 10,
			    pageList : [10, 20, 30],
			    url : '../AdminRoleType/list.json',
				loadMsg : '数据装载中......',
				singleSelect : true,//设置为 true，则只允许选中一行	
				toolbar: [{
					text: '新增 角色',
					iconCls: 'icon-add',
					handler: function(){	
						art.dialog.open('../AdminRoleType/addInterface.htm?type=add',{
									title: '',
									lock:true,
								  	width: '50%',
								    height: '50%'												   												   
								});
					}
				}],	
				columns:[[					          	
							{field:'id',hidden:true,editor:{type:'text'}},
							{field:'roleName',title:'角色名称',width:60,align:'center',editor:{type:'text'}},						
							{field:'status',title:'启用',width:20,align:'center',editor:{type:'text'},
								 formatter:function(value,row,index){						    	
								    	if (value){
								    		var ss;
								    		if(value){
								    			ss='<span disabled="disabled"><input type="checkbox" checked="checked" disabled="disabled"></span> ';
								    		}else{
								    			ss='<span disabled="disabled"><input type="checkbox" disabled="disabled"></span> ';
								    		}
									    	return ss;
								    	} else {
									    	return '';
								    	}
							    	}
							},	
							{field:'isAdmin',title:'管理员',width:20,align:'center',editor:{type:'text'},
								 formatter:function(value,row,index){						    	
								    	if (value){
								    		var ss;
								    		if(value){
								    			ss='√';
								    		}else{
								    			ss='';
								    		}
									    	return ss;
								    	} else {
									    	return '';
								    	}
							    	}
							},
							{field:'opt',title:'操作',width:80,align:'center',  
			                    formatter:function(value,rec,index){
			                    	
			                    	var edit='<a title="编辑" onclick="edit(\''+ rec.id + '\')"  href="#">'+
			                    			'<img src="<%=basePath %>pages/Admin/Images/Icon/edit.gif" alt="Edit"></a>&nbsp;&nbsp;';
			                    	
			                        var dele='<a title="删除" onclick="del(\''+ rec.id +'\')"  href="#">'+
			                        		'<img src="<%=basePath %>pages/Admin/Images/Icon/delete.gif" alt="Delete"></a>&nbsp;&nbsp;';				                        
			                        
			                        var cog='<a title="设置权限" onclick="addCog(\''+ rec.id +'\',\''+rec.roleName+'\')"  href="#">设置权限'+
			                        		'<img src="<%=basePath %>pages/Admin/Images/Icon/cog.gif" alt="Delete"></a>&nbsp;&nbsp;';				                        
			                       
			                        var addUser='<a title="添加人员" onclick="addUser(\''+ rec.id +'\',\''+rec.roleName+'\')"  href="#">选择人员'+
			                        		'<img src="<%=basePath %>pages/Admin/Images/Icon/add.gif" alt="Delete"></a>&nbsp;&nbsp;';				                        
			                                              
			                        return edit+dele+cog+addUser;  
			                    }  
			                 }								
				        ]]	
			});			
			
			//加载人员
			$('#dataGridUsers').datagrid({		
				height:'455',
				width:'770',
				rownumbers:true,//设置为true将显示行数					
				striped:true, //设置为true将交替显示行背景
				fitColumns:true,//设置为true将自动使列适应表格宽度以防止出现水平滚动
				animate:true,
				collapsible:true,								
				idField:'id',//id字段	
				pagination : true,//分页控件
				pageSize : 10,
			    pageList : [10, 20, 30],
			    url : null,
				loadMsg : '数据装载中......',
				singleSelect : false,//设置为 true，则只允许选中一行				
				columns:[[					          	
							{ field:'ck',checkbox:true },
							{field:'id',hidden:true,editor:{type:'text'}},
							{field:'userName',title:'用户名',width:100,align:'center',editor:{type:'text'}},
							{field:'realName',title:'真实姓名',width:100,align:'center',editor:{type:'text'}},
							{field:'roleId',hidden:true,title:'角色ID',width:100,align:'center',editor:{type:'text'}},	
							{field:'roleName',title:'角色',width:100,align:'center',editor:{type:'text'}},													
							{field:'orgId',hidden:true,title:'组织架构ID',width:100,align:'center',editor:{type:'text'}},
							{field:'orgName',title:'组织架构名称',width:100,align:'left',editor:{type:'text'}}
						]],				
			    onClickRow: function(rowIndex, rowData){
			        //加载完毕后获取所有的checkbox遍历
			        $("#roleGetUser input[type='checkbox']").each(function(index, el){
			            //如果当前的复选框不可选，则不让其选中
			            if (el.disabled == true) {
			                $('#dataGridUsers').datagrid('unselectRow', index - 1);
			            }
			        });
			    },
	            onCheckAll: function(rows) {  
	                $("#roleGetUser input[type='checkbox']").each(function(index, el) {  	                      
	                    if (el.disabled) {  
	                        $("#dataGridUsers").datagrid('uncheckRow', index - 1);	                         
	                    }  
	                }); 
	            },
			});							
			
			//添加菜单 到 角色 
			$('#dataGridMenu').treegrid({								
				height:'455',		
				width:'850',
				rownumbers: true,
				animate:true,
				collapsible:true,
				fitColumns:true,					
				url:null,
				method: 'post',
				idField: 'id',
				treeField: 'name',			
				showFooter:true,
				singleSelect : false,
				checkOnSelect:true,
				selectOnCheck:true,				
				//onLoadSuccess: function () {$('#tg').treegrid('collapseAll')},
				toolbar: [{
	                text : '保存权限',
	                iconCls : 'icon-save',
	                handler : function() {
	                	var roleTypeId=$('#menuRoleType').val();			
	        			var checkedItems = $('#dataGridMenu').treegrid('getChecked');						
	        			var names = [];						
	        			$.each(checkedItems, function(index, item){						
	        				names.push(item.id);						
	        			}); 									
	        			//AdminRoleMenu
	        			$.ajax({
	        				type:'POST',
	        				dataType:'json',
	        				async:false,
	        				url:'../AdminRoleMenu/save.json',
	        				data:{ids:names.join(","),roleId:roleTypeId},
	        				success:function(data){
	        					if(data.success=true){	
	        						art.dialog({id : 'Tip',title : '消息',fixed : true,lock : true,opacity: .2,
	        							content : '保存成功！',icon : 'succeed',time : 1  
	        					    }); 
	        						$('#dataGridMenu').treegrid('reload');									
	        					}else{
	        						art.dialog.alert('保存失败，请联系管理员！');
	        					}
	        				}
	        			});
	        			
	                }
	            }, '-', {
	                text : '展开',
	                iconCls : 'icon-redo',
	                handler : function() {
	                    var node = $('#dataGridMenu').treegrid('getSelected');		                  
	                    if (node) {		                    	
	                        $('#dataGridMenu').treegrid('expand', node.id);
	                    } else {		                    	
	                        $('#dataGridMenu').treegrid('expandAll');
	                    }
	                }
	            }, '-', {
	                text : '折叠',
	                iconCls : 'icon-undo',
	                handler : function() {
	                    var node = $('#dataGridMenu').treegrid('getSelected');
	                    if (node) {
	                        $('#dataGridMenu').treegrid('collapseAll', node.id);
	                    } else {
	                        $('#dataGridMenu').treegrid('collapseAll');
	                    }
	                }
	            }, '-', {
	                text : '刷新',
	                iconCls : 'icon-reload',
	                handler : function() {
	                    $('#dataGridMenu').treegrid('reload');
	                }
	            }],					
				columns:[[
							{field:'ck',checkbox:true},
							{title:'id',field:'id',hidden:true,width:10,
								formatter:function(value,row){									
									if(row.checked){
										$('#dataGridMenu').treegrid('select',value); //根据id来选择的 不是行		
									}
								}	
							},
							{title:'parentID',field:'parentID',hidden:true,width:10},
							{title:'名称',field:'name',width:100},														
							{title:'图标',field:'icon',width:40},
							{title:'路径',field:'url',width:100},						
							{title:'排序',field:'sortID',width:20},						
							{title:'显示',field:'visible',width:20,align:'center',
							    formatter:function(value){						    	
							    	if (value){
							    		var ss;
							    		if(value=="1"){
							    			ss='<span disabled="disabled">√</span> ';
							    		}
								    	return ss;
							    	} else {
								    	return '';
							    	}
						    	}
							}					  
					
				]],  
				onClickRow:function(row){  //当用户点击一个节点时触发。					
				    //级联选择  
				    $(this).treegrid('cascadeCheck',{  
				        id:row.id, //节点ID  
				        deepCascade:false//深度级联  
				    });  
				}
				
			});
			
						
		});
		
		function edit(index){  //编辑  
			art.dialog.open('../AdminRoleType/updateInterface.htm?type=edit&id='+index,{
				title: '',
				lock:true,
			  	width: '50%',
			    height: '50%'												   												   
			});
      	}
		
		function del(index){  //删除操作
			art.dialog({
				icon: 'question',	
				width:250,
			    content: '你确定删除该数据？',
			    ok: function () {				    	
			    	$.ajax({
						type:'POST',
						dataType:'json',
						async:false,
						url:'../AdminRoleType/delete.json',
						data:{ids:index,delSub:true},
						success:function(data){
							if(data.success=true){									
								art.dialog({id : 'Tip',title : '消息',fixed : true,lock : true,opacity: .2,
        							content : '删除成功！',icon : 'succeed',time : 1  
        					    }); 
								window.location.reload();  
								
							}else{
								art.dialog.alert('删除失败，请联系管理员！');
							}
						}
					});
					
			    },
			    cancelVal: '取消',
			    cancel: true 
			});				
      	}
		
		//添加人员到角色 
		function addUser(roleTypeId,roleName){  
			$('#roleGetMenu').css('display','none');
			$('#roleGetUser').css('display','block');
			$('#roleType').val(roleTypeId);
			$('.notes').html("请为角色<strong>["+roleName+"]</strong>选择用户！&nbsp;&nbsp;");	
			
			//加载人员
			$('#dataGridUsers').datagrid({					
			    url : '../AdminUser/getUserList.json?roleType='+roleTypeId,				
				onLoadSuccess:function(row){//当表格成功加载时执行						
	                var rowData = row.rows;
	                $.each(rowData,function(idx,val){//遍历JSON
	                	  if(val.roleId!=null && val.roleId!=roleTypeId){	                		 
	                		  $("#roleGetUser input[type='checkbox']")[idx + 1].disabled = true;
	                	  }else  if(val.roleId==roleTypeId){
	                		  $('#dataGridUsers').datagrid("selectRow", idx);//如果数据行为已选中则选中改行
	                      }	                	  
	                });              
	            }			    
			});				
			$("#dataGridUsers").datagrid('clearSelections').datagrid('clearChecked');
			
			//$('#dataGridUsers').datagrid('load'); //回到第一页		
			//$('#dataGridUsers').datagrid('options').pageNumber=1;
			//结束人员加载
      	}
		function searchUserList() {
			var queryParams = $('#dataGridUsers').datagrid('options').queryParams;	
			var orgIds=$('#orgId').combotree("getValues").join(',');			
			queryParams.userName = $('#userName').val();
			queryParams.orgId =orgIds ;				
			$('#dataGridUsers').datagrid('reload');
			//$('#dataGridUsers').datagrid('clearSelections');
		}
		
		function addUsertoRole(){						
			var roleTypeId=$('#roleType').val();			
			var checkedItems = $('#dataGridUsers').datagrid('getChecked');						
			var names = [];						
			$.each(checkedItems, function(index, item){						
				names.push(item.id);						
			}); 									
			
			$.ajax({
				type:'POST',
				dataType:'json',
				async:false,
				url:'../AdminUserRole/save.json',
				data:{ids:names.join(","),roleType:roleTypeId},
				success:function(data){
					if(data.success=true){									
						art.dialog({id : 'Tip',title : '消息',fixed : true,lock : true,opacity: .2,
							content : '保存成功！',icon : 'succeed',time : 1  
					    }); 						
						$('#dataGridUsers').datagrid('reload');									
					}else{
						art.dialog.alert('保存失败，请联系管理员！');
					}
				}
			});		
		}
		
		function addCog(roleTypeId,roleName){			
			$('#roleGetUser').css('display','none');
			$('#roleGetMenu').css('display','block');
			$('#menuRoleType').val(roleTypeId);
			$('#roleGetMenu .notes').html("请为角色<strong>["+roleName+"]</strong>选择权限！&nbsp;&nbsp;");				
			//加载权限
			$('#dataGridMenu').treegrid({	
				url : '../AdminMenu/getEasyUiTree.json?roleId='+roleTypeId,	
			});				
			$('#dataGridMenu').treegrid('clearSelections').treegrid('clearChecked');
		}
				
		</script>
		
		<style>	  
			.notes {
			    padding: 5px;			  
			    color: #ccc;
			    background: #fffef7;
			    border: 1px solid #FBDA6D;
			}
			
			.notes strong{	
				color:#bc9610;
			}
  
   	 </style>
   	 
	</head>
<body>
	<div style="float:left;-webkit-box-shadow:0 0 10px 5px #fafafa; -moz-box-shadow:0 0 10px 5px #fafafa; box-shadow:0 0 10px 5px #fafafa;  ">
		<table id="dataGridSys" width="100%" height="100%"></table>	
	</div>
	
	<!--角色选择用户-->	
	<div id="roleGetUser" style="float:left;margin-left:5px;display:none;border: 1px solid #0092dc;border-radius: 5px;">
		<p class="notes"></p>	
		<table id="dataGridUsers" type="dataGrid"	data-options="toolbar:'#toolBar'">
		</table>		
		<div id="toolBar" style="height: auto;padding:0px;">
			<!--表单  搜索栏-->
			<div id="filter" class="module">					
				<ul>				 
					<li>
						<input type="hidden" id="roleType" value="0"/>				
					</li>									
					<li>
						<label>用户名：</label>
						<input type="text" value="" id="userName"/>						
					</li>
					<li>
						<label>组织架构：</label>
						<select id="orgId" name="orgId" class="easyui-combotree" multiple style="width:250px;"   
        			data-options="url:'../district/findByPidAll.json',panelHeight:245"></select>						
					</li>
					<li><input type="button" class="btn btn-blue-h20-w2" value="查询" onclick="searchUserList()" /></li>
				</ul>
				<div class="clear"></div>
			</div>
			<!--表单  列表栏-->
			<div class="list module">
				<div class="hd">
					<h2 class="t">数据列表</h2>
					<span class="handle4filter" title="显示/隐藏查询条件"></span>
				</div>
				<div class="bd">
					<div id="toolbar">
						<ul id="buttonPlace">	
							<li style="paddding-right:0px;">
								<a href="javascript:addUsertoRole()" class="l-btn l-btn-small l-btn-plain">
									<span class="l-btn-left l-btn-icon-left">
										<span class="l-btn-text">保存到角色</span>
										<span class="l-btn-icon icon-save">&nbsp;</span>
									</span>
								</a>
							</li>							
						</ul>
					</div>
				</div>
			</div>
		</div>		
		<div id="win"></div>
	</div>
	<!--角色选择菜单-->	
	<div id="roleGetMenu" style="float:left;margin-left:5px;display:none;border: 1px solid #0092dc;border-radius: 5px;">
		<p class="notes"></p>
		<input type="hidden" id="menuRoleType" value="0"/>
		<table id="dataGridMenu">
		</table>
	</div>
	
	

</body>
</html>